<?php
/*
 * 大宗商品
 */
class CommodityMode extends SiteMode {
    /** @return CommodityMode */
    public static function init() {
        return parent::init();
    }
    //添加一条报备或团购
    function addRecord($gopen_id,$user_id,$name,$tel,$type = 'record'){
        $data['gopen_id']=$gopen_id;
        $data['user_id']=$user_id;
        $data['name']=$name;
        $data['tel']=$tel;
        $data['type']=$type;
        $data['site_id']=$this->siteId;
        return $this->db->insert('goods_record',$data)==1 ? (int)$this->db->insertId():-1;
    }
    //报备进度列表
    function getList($user_id = null ,$rows = 30,$offset = 0,$type=null){
        $rows=(int)$rows;
        $offset=(int)$offset;
        $data=array(
            'goods_record GR'=>array('*'),
            'goods G'=>array('name'=>'goods_name'),
            'user U'=>array('username','nickname')
        );
        $condition=array(
            'GR.`user_id`=U.`id`',
            'GR.`gopen_id`=G.`gopen_id`',
            `GR.site_id`=>$this->siteId,
            'G.enable'=>1,
        );
        $condition['GR.user_id']=$user_id;
        if($type!='record' || $type!='group') $type='';
        $condition['GR.type']=$type;
        $condition['site_id']=$this->siteId;
        $sql=$this->db->selectSql($data,$condition,$rows,$offset,`GR.create_time`,false);
        return $this->db->getList($sql);
    }
    //根据gopen_id查询该商家大宗商品报备情况
    function getListByGopenId($gopen_id = null ,$rows = 30,$offset = 0,$type=null){
        $rows=(int)$rows;
        $offset=(int)$offset;
        switch($type){
            case 'record':break;
            case 'group':break;
            default:$type='';
        }
        $data=array(
            'goods_record GR'=>array('*'),
            'user U'=>array('username','nickname')
        );
        $condition=array(
            'GR.`user_id`=U.`id`',
            'GR.site_id'=>$this->siteId,
        );
        $condition['GR.gopen_id']=$gopen_id;
        $condition['GR.type']=$type;
        $sql=$this->db->selectSql($data,$condition,$rows,$offset,`GR.create_time`,false);
        return $this->db->getList($sql);
    }
    //设置报备状态
    function setState($id,$state){
        $data['state']=$state;
        return $this->db->update('goods_record',$id,$data)==1;
    }
    //设置是否购买
    function setBuy($id,$buy = 1){
        $data['buy']=$buy;
        return $this->db->update('goods_record',$id,$data,true)==1;
    }
    function delete($id){
        return $this->db->delete('goods_record',$id)==1;
    }
    //手机号合法：没有被报备，或报备已经解禁,或用户自己没有团购
    function checkTelValid($tel){
        $today=$this->db->quote(date('Y-m-d H:i:s',time()));
        $sql="SELECT * FROM `goods_record`
              WHERE ((`type`='record' AND To_Days($today)-To_Days(`create_time`)<=15)
               OR (`type`='group')) AND `tel`=?";
        $re=$this->db->getOne($sql,$tel);
        if(!empty($re)){
            return "该手机号已经可能报备并处于有效期内或者已申请团购,不能多次报备";
        }else{
            return "success";
        }
    }
    //团购检查手机号
    function checkGroupTelValid($tel){
        $today=$this->db->quote(date('Y-m-d H:i:s',time()));
        $sql="SELECT * FROM `goods_record` WHERE `tel`=? and site_id=?
            AND (`type`='group' AND To_Days($today)-To_Days(`create_time`)<=15)";
        $re=$this->db->getOne($sql,$tel, $this->siteId);
        if(!empty($re)){
            return "该手机号已经申请团购并处于有效期内,不能多次团购";
        }else{
            return "success";
        }
    }
} 